
package com.hexinfo.dmpro.sparing.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.model.ClusterSparingConf;
import com.hexinfo.dmpro.common.model.MessageCenter;
import com.hexinfo.dmpro.common.service.MessageCenterService;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.common.utils.ScanCommonConstants;
import com.hexinfo.dmpro.sparing.dao.SyncRuleConfMapper;
import com.hexinfo.dmpro.sparing.dto.*;
import com.hexinfo.dmpro.sparing.model.*;
import com.hexinfo.dmpro.sparing.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 删除2年外数据
 *
 * @author yemw
 * @date 2023-09-08 17:45:29
 */
@Service
@Slf4j
public class SyncRuleConfServiceTHREEImpl extends ServiceImpl<SyncRuleConfMapper, SyncRuleConf> implements SyncRuleConfTHREEService {
    @Value("${hdfs.tarPrefix}")
    private String prefix;
    @Autowired
    private ScanMetadataService scanMetadataService;
    @Autowired
    private TimeFieldMatchingService timeFieldMatchingService;
    @Autowired
    private HiveToOracleService hiveToOracleService;
    @Autowired
    private MessageCenterService messageCenterService;
    @Autowired
    private AvailabilityPathService availabilityPathService;
    @Autowired
    private ImpalaService impalaService;
    @Autowired
    private HotSpareDeleService hotSpareDeleService;
    @Autowired
    private HotSpareDeleTaskService hotSpareDeleTaskService;

    private SyncRuleConfServiceImpl syncRuleConfServiceImpl;


    /**
     * 全源
     * @return
     */
    @Override
    public Boolean allSource() {
        //任务表
        HotSpareDeleTask hotSpareDeleTask = new HotSpareDeleTask();
        hotSpareDeleTask.setTaskName("万国全源"+DateUtil.format(new Date(), "yyyyMMdd"));
        hotSpareDeleTask.setSourceName("全源");
        hotSpareDeleTask.setTaskState(ScanCommonConstants.ClusterName.JXZ.value);
        hotSpareDeleTaskService.save(hotSpareDeleTask);

        LambdaQueryWrapper<SyncRuleConf> syncRuleWrapper = Wrappers.lambdaQuery();
        syncRuleWrapper.select(SyncRuleConf::getSourceName).eq(SyncRuleConf::getDel, CommonConstants.STATUS_NORMAL).orderByDesc(SyncRuleConf::getSourceName).groupBy(SyncRuleConf::getSourceName);
        List<Object> syncRuleConfs = this.baseMapper.selectObjs(syncRuleWrapper);
        for (Object syncRuleConf: syncRuleConfs) {
            HandMoveSyncDTO dto = new HandMoveSyncDTO();
            dto.setHotSpareDeleTask(hotSpareDeleTask);
            if(ObjectUtil.isEmpty(syncRuleConf)){
                dto.setSource("无源");
            }else{
                dto.setSource(syncRuleConf.toString());
            }
            syncDele(dto);
        }
        hotSpareDeleTask.setTaskState(ScanCommonConstants.ClusterName.YWC.value);
        hotSpareDeleTaskService.updateById(hotSpareDeleTask);
        return true;
    }


    /**
     * 同步删除两年外的数据
     * @param dto
     * @return
     */
    @Override
    public Boolean syncDele(HandMoveSyncDTO dto) {
        String nameNodeStatus = availabilityPathService.getSouAndTar();
        if (StrUtil.isBlank(nameNodeStatus)){
            log.warn("-------------------高可用节点变动异常");
            return false;
        }
        Map<String,String> map = new HashMap<>();
        //消息中心
        MessageCenter mess = new MessageCenter();
        mess.setMenu(ScanCommonConstants.ClusterName.SJRB.value);
        mess.setOperator("admin");
        mess.setDataType(ScanCommonConstants.ClusterName.JXZ.value);
        mess.setMessageSubject(ScanCommonConstants.ClusterName.SCSJJH.value);
        mess.setMessageWeight(ScanCommonConstants.ClusterName.ONE.value);
        messageCenterService.save(mess);
        //查询规则
        LambdaQueryWrapper<SyncRuleConf> syncRuleWrapper = Wrappers.lambdaQuery();
        syncRuleWrapper.eq(SyncRuleConf::getDel, CommonConstants.STATUS_NORMAL)
//                .eq(SyncRuleConf::getExeFrequency, ScanCommonConstants.ClusterName.ONE.value)
                .eq(StrUtil.isNotBlank(dto.getSource())&&!"无源".equals(dto.getSource()),SyncRuleConf::getSourceName,dto.getSource())
                .isNull(StrUtil.isBlank(dto.getSource())||"无源".equals(dto.getSource()),SyncRuleConf::getSourceName);
        List<SyncRuleConf> syncRuleConfs = this.baseMapper.selectList(syncRuleWrapper);
        //查询集群信息
        LambdaQueryWrapper<ClusterSparingConf> jQWrapper = Wrappers.lambdaQuery();
        LambdaQueryWrapper<ClusterSparingConf> wGWrapper = Wrappers.lambdaQuery();
        ClusterSparingConf jQCluster = scanMetadataService.wrapperClusterSparingConf(jQWrapper,"金桥");
        ClusterSparingConf wGCluster = scanMetadataService.wrapperClusterSparingConf(wGWrapper,"万国");
        //DistCP集合
        List<HdfsHeatBackup> hdfsHeatBackups = new ArrayList<>();
        //对比条件集合
        List<WhereScriptDTO> whereScriptDTOS = new ArrayList<>();
        RuleToDistCPDTO ruleToDistCPDTO = new RuleToDistCPDTO();
        syncRuleConfs.forEach(syncRule -> {
            ScanSyncRuleDTO syncRuleDTO = syncRuleConfServiceImpl.splQuSoeDataTable(syncRule);
            //判断同步范围 全量还是近2年 0为全量
            //全量数据不需要判断时间分区
            if(syncRule.getSyncRange()==0){
                log.warn("================当前规则为:"+syncRule.getSyncRuleName());
                List<ScanMetadata> scanMetadataList = scanMetadataService.qyScanMetadataWhereWord(syncRuleDTO);
                scanMetadataList.forEach(s ->{
                    log.warn("==========当前表为:"+s.getTblName());
                    if("源标识同步".equals(syncRule.getSyncMode())&&"分区".equals(syncRule.getIsPart())&&"无时间分区".equals(syncRule.getIsTimePart())){
                        //拼接条件
                        String where = syncRuleConfServiceImpl.whereLike(syncRule);
                        //查询该表源的hdfs路径
                        List<String> hdfsList = hiveToOracleService.selectHiveLocation(where,s.getTblName(),s.getTblDatabaseName());
                        if(hdfsList!=null&&hdfsList.size()>0){
                            for (String hdfs: hdfsList) {
                                HdfsHeatBackup kup = new HdfsHeatBackup();
                                WhereScriptDTO whereScriptDTO = new WhereScriptDTO();
                                String lo = hdfs.substring(syncRuleConfServiceImpl.getSlashIndex(hdfs,6) + 1);
                                kup.setSourceAddress(jQCluster.getHdfsPrefixPath()+lo);
                                kup.setTargetAddress(wGCluster.getHdfsPrefixPath()+prefix+lo);
                                kup.setSourceDbName(s.getTblDatabaseName());
                                kup.setTargetDbName(prefix+s.getTblDatabaseName());
                                kup.setSourceTableName(s.getTblName());
                                kup.setTargetTableName(s.getTblName());
                                kup.setBackupSort("1");
                                ruleToDistCPDTO.setBackupSort("1");
                                whereScriptDTO.setSourceDbName(s.getTblDatabaseName());
                                whereScriptDTO.setSourceTableName(s.getTblName());
                                kup.setSourceData(syncRule.getSourceName());
                                ruleToDistCPDTO.setSourceDataName(syncRule.getSourceName());
                                String[] partNames = hdfs.substring(syncRuleConfServiceImpl.getSlashIndex(hdfs,8) + 1).split("=");
                                whereScriptDTO.setSourceQueryCriteria(StrUtil.replace(StrUtil.replace(hdfs.substring(syncRuleConfServiceImpl.getSlashIndex(hdfs,8) + 1),"=","='"),partNames[0],"cast( "+ partNames[0] +" as string )") + "'");
                                whereScriptDTO.setTargetQueryCriteria(whereScriptDTO.getSourceQueryCriteria());
                                whereScriptDTO.setTargetTableName(s.getTblName());
                                whereScriptDTO.setTargetDbName(prefix+s.getTblDatabaseName());
                                whereScriptDTOS.add(whereScriptDTO);
                                hdfsHeatBackups.add(kup);
                                log.warn("==========================【全量源标识】当前数据比对条件"+whereScriptDTO.getSourceQueryCriteria());
                            }
                        }
                    }else {
                        WhereScriptDTO whereScriptDTO = new WhereScriptDTO();
                        HdfsHeatBackup kup = new HdfsHeatBackup();
                        if("分区".equals(syncRule.getIsPart())&&"特殊表含时间分区".equals(syncRule.getIsTimePart())){
                            kup.setSourceAddress(jQCluster.getHdfsPrefixPath()+s.getTblDatabaseName()+".db/"+s.getTblName()+"/"+syncRule.getPartValueWhite());
                            kup.setTargetAddress(wGCluster.getHdfsPrefixPath()+prefix+s.getTblDatabaseName()+".db/"+s.getTblName()+"/"+syncRule.getPartValueWhite());
                            String[] partNames = syncRule.getPartValueWhite().split("=");
                            //当有时间分区时对比条件拼接要加上时间分区
                            if(StrUtil.isBlank(syncRule.getPartNameWhite())){
                                whereScriptDTO.setSourceQueryCriteria(StrUtil.replace(StrUtil.replace(syncRule.getPartValueWhite(),"=","='"),partNames[0],"cast( "+ partNames[0] +" as string )") + "'");
                            }else{
                                LambdaQueryWrapper<TimeFieldMatching> timeFieldWrapper = Wrappers.lambdaQuery();
                                timeFieldWrapper.select(TimeFieldMatching::getTimeFieldKay, TimeFieldMatching::getTimeFieldValue);
                                timeFieldWrapper.eq(TimeFieldMatching::getTimeFieldKay, syncRule.getPartNameWhite())
                                        .eq(TimeFieldMatching::getDel, CommonConstants.STATUS_NORMAL);
                                TimeFieldMatching matching = timeFieldMatchingService.getOne(timeFieldWrapper);
                                String endTime = DateUtil.format(DateUtil.date(), matching.getTimeFieldValue());
                                int betweenDay = -7500;
                                String staTime = DateUtil.format(DateUtil.offset(DateUtil.date(), DateField.DAY_OF_MONTH, betweenDay), matching.getTimeFieldValue());
                                whereScriptDTO.setSourceQueryCriteria(StrUtil.replace(StrUtil.replace(syncRule.getPartValueWhite(),"=","='"),partNames[0],"cast( "+ partNames[0] +" as string )") + "' and "+"cast( "+ syncRule.getPartNameWhite() +" as int )"+ " BETWEEN " + staTime +" and "+ endTime);
                            }
                            whereScriptDTO.setTargetQueryCriteria(whereScriptDTO.getSourceQueryCriteria());
                        }else {
                            kup.setSourceAddress(jQCluster.getHdfsPrefixPath()+s.getTblDatabaseName()+".db/"+s.getTblName());
                            kup.setTargetAddress(wGCluster.getHdfsPrefixPath()+prefix+s.getTblDatabaseName()+".db/"+s.getTblName());
                        }
                        kup.setSourceDbName(s.getTblDatabaseName());
                        kup.setTargetDbName(prefix+s.getTblDatabaseName());
                        kup.setSourceTableName(s.getTblName());
                        kup.setTargetTableName(s.getTblName());
                        kup.setBackupSort("1");
                        ruleToDistCPDTO.setBackupSort("1");
                        whereScriptDTO.setSourceDbName(s.getTblDatabaseName());
                        whereScriptDTO.setSourceTableName(s.getTblName());
                        if(StrUtil.isNotBlank(syncRule.getSourceName())){
                            kup.setSourceData(syncRule.getSourceName());
                            ruleToDistCPDTO.setSourceDataName(syncRule.getSourceName());
                        }else{
                            kup.setSourceData("无源");
                            ruleToDistCPDTO.setSourceDataName("无源");
                        }
                        if(StrUtil.isBlank(whereScriptDTO.getSourceQueryCriteria())){
                            whereScriptDTO.setSourceQueryCriteria("");
                            whereScriptDTO.setTargetQueryCriteria(whereScriptDTO.getSourceQueryCriteria());
                        }
                        whereScriptDTO.setTargetTableName(s.getTblName());
                        whereScriptDTO.setTargetDbName(prefix+s.getTblDatabaseName());
                        whereScriptDTOS.add(whereScriptDTO);
                        log.warn("==========================【常规/特殊】当前数据比对条件"+whereScriptDTO.getSourceQueryCriteria());
                        hdfsHeatBackups.add(kup);
                    }
                });
            //近2年/近几年数据一定包含时间分区
            }else if(syncRule.getSyncRange()>0){
                int betweenDay = syncRule.getSyncRange();
                log.warn("================当前规则为:"+syncRule.getSyncRuleName());
                //分组拿表名
                List<ScanMetadata> scanMetadataList = scanMetadataService.qyScanMetadataWhereWord(syncRuleDTO);
                scanMetadataList.forEach(s ->{
                    log.warn("==========当前表为:"+s.getTblName());
                    //查询该表的全部分区字段
                    List<String> hdfsList = hiveToOracleService.selectHivePkeyName(s.getTblName(),s.getTblDatabaseName());
                    //循环匹配分区字段配置表 拿到字段名称和字段格式
                    for (String h : hdfsList) {
                        LambdaQueryWrapper<TimeFieldMatching> timeFieldWrapper = Wrappers.lambdaQuery();
                        timeFieldWrapper.select(TimeFieldMatching::getTimeFieldKay, TimeFieldMatching::getTimeFieldValue);
                        timeFieldWrapper.eq(TimeFieldMatching::getTimeFieldKay, h).eq(TimeFieldMatching::getDel, CommonConstants.STATUS_NORMAL);
                        TimeFieldMatching matching = timeFieldMatchingService.getOne(timeFieldWrapper);
                        //不为空就匹配成功
                        if (ObjectUtils.isNotEmpty(matching)) {
                            //拼接条件
                            String where = syncRuleConfServiceImpl.whereLike(syncRule);
                            //获取当前规则源2年分区的hdfs地址
                            List<String> locationList = hiveToOracleService.selectHiveTableHDFS(s.getTblName(), where, matching.getTimeFieldKay(), -betweenDay, matching.getTimeFieldValue(), s.getTblDatabaseName());
                            //拼接数据对比所需条件 出现类似于订单TA源分区多种value值
                            WhereScriptDTO whereScriptDTO =  syncRuleConfServiceImpl.whereScript(locationList, s.getTblDatabaseName(), s.getTblName(), matching, syncRule.getSyncMode(), -betweenDay, prefix);
                            Set<kvDTO> locationGorup = new HashSet<>();
                            locationList.forEach(lo -> {
                                HdfsHeatBackup kup = new HdfsHeatBackup();
                                //切割分区信息
                                String partName = "";
                                if (StrUtil.split(lo, s.getTblName()).length == 2) {
                                    partName = StrUtil.split(lo, s.getTblName())[1];
                                } else {
                                    log.warn("===========" + lo + "===========");
                                }
                                //（为了避免该表有多个时间分区字段） 且不等于无源
                                if(StrUtil.isNotBlank(partName)&&StrUtil.isNotBlank(syncRule.getSourceName())){
                                    kvDTO kvDTO = new kvDTO();
                                    //判断时间分区在前在后 切割源标识
                                    String[] names = partName.substring(1,partName.length()).split("/");
                                    String[] partNamesQ = names[0].split("=");
                                    if(names.length>=2){
                                        String[] partNamesH = names[1].split("=");
                                        //分区在前
                                        if(matching.getTimeFieldKay().equals(partNamesQ[0])){
                                            kvDTO.setSourceKey(names[1]);
                                            kvDTO.setSourceValue(partNamesH[0]);
                                            kvDTO.setTimeKey(partNamesQ[0]);
                                            locationGorup.add(kvDTO);
                                            //分区在后
                                        }else if(matching.getTimeFieldKay().equals(partNamesH[0])){
                                            kvDTO.setSourceKey(names[0]);
                                            kvDTO.setSourceValue(partNamesQ[0]);
                                            kvDTO.setTimeKey(partNamesH[0]);
                                            locationGorup.add(kvDTO);
                                        }
                                    }else{
                                        kvDTO.setSourceKey(names[0]);
                                        locationGorup.add(kvDTO);
                                    }
                                }
                                lo = jQCluster.getHdfsPrefixPath() + s.getTblDatabaseName() + ".db/" + s.getTblName() + partName;
                                kup.setSourceAddress(lo);
                                kup.setTargetAddress(StrUtil.replace(StrUtil.replace(lo, jQCluster.getHdfsPrefixPath(), wGCluster.getHdfsPrefixPath()), "/" + s.getTblDatabaseName() + ".db/", "/" + prefix + s.getTblDatabaseName() + ".db/"));
                                kup.setSourceDbName(s.getTblDatabaseName());
                                kup.setTargetDbName(prefix + s.getTblDatabaseName());
                                kup.setSourceTableName(s.getTblName());
                                kup.setTargetTableName(s.getTblName());
                                kup.setPkeyName(matching.getTimeFieldKay());
                                kup.setSyncRange(syncRule.getSyncRange());
                                kup.setBackupSort("1");
                                ruleToDistCPDTO.setBackupSort("1");
                                if (StrUtil.isNotBlank(syncRule.getSourceName())) {
                                    kup.setSourceData (syncRule.getSourceName());
                                    ruleToDistCPDTO.setSourceDataName(syncRule.getSourceName());
                                } else {
                                    kup.setSourceData("无源");
                                    ruleToDistCPDTO.setSourceDataName("无源");
                                }
                                hdfsHeatBackups.add(kup);
                            });
                            //源分区单种value值（为了避免该表有多个时间分区字段） 或者无源
                            if((locationGorup!=null&&locationGorup.size()==1)||StrUtil.isBlank(syncRule.getSourceName())){
                                log.warn("==========================【源分区单种value值】当前数据比对条件"+whereScriptDTO.getSourceQueryCriteria());
                                whereScriptDTOS.add(whereScriptDTO);
                                break;
                                //源分区多种value值
                            }else if(locationGorup!=null&&locationGorup.size()>1){
                                for (kvDTO kv : locationGorup) {
                                    WhereScriptDTO wsDTO = new WhereScriptDTO();
                                    String endTime = DateUtil.format(DateUtil.date(), matching.getTimeFieldValue());
                                    String staTime =  DateUtil.format(DateUtil.offset(DateUtil.date(), DateField.DAY_OF_MONTH, -betweenDay), matching.getTimeFieldValue());
                                    String whereSql = StrUtil.replace(StrUtil.replace(kv.getSourceKey(),"="," = '"),kv.getSourceValue(),"cast( "+ kv.getSourceValue() +" as string )") + "' and "+"cast( "+ kv.getTimeKey() +" as int )"+ " BETWEEN " + staTime +" and "+ endTime;
                                    wsDTO.setSourceDbName(s.getTblDatabaseName());
                                    wsDTO.setSourceTableName(s.getTblName());
                                    wsDTO.setTargetDbName(prefix+s.getTblDatabaseName());
                                    wsDTO.setTargetTableName(s.getTblName());
                                    wsDTO.setSourceQueryCriteria(whereSql);
                                    wsDTO.setTargetQueryCriteria(wsDTO.getSourceQueryCriteria());
                                    log.warn("==========================【源分区多种value值】当前数据比对条件"+wsDTO.getSourceQueryCriteria());
                                    whereScriptDTOS.add(wsDTO);
                                }
                            }
                        }
                    }
                });
            }
        });
        //将比对sql对象 赋值给传输DTO
        ruleToDistCPDTO.setWhereScriptDTOS(whereScriptDTOS);
        ruleToDistCPDTO.setHdfsHeatBackups(hdfsHeatBackups);
        //同步删除近2年
        boolean bol = sqlImpalaDele(ruleToDistCPDTO,prefix,dto.getHotSpareDeleTask());
        mess.setDataType(ScanCommonConstants.ClusterName.YWC.value);
        messageCenterService.updateById(mess);
        return bol;
    }

    @Override
    public Boolean sqlImpalaDele(RuleToDistCPDTO dto,String tarPrefix,HotSpareDeleTask hotSpareDeleTask) {
        String auditSql = "select count(1) as count from {} where 1=1 {} and cast({} as int) between {} and {}";
        String auditSqlNot = "select count(1) as count from {} where 1=1 {}";
        String dropSql = "alter table {} drop if exists partition ({} cast({} as int) < {})";
        String delCol = "cast({} as string) = '{}' ,";
        String auditCol = "and cast({} as string) = '{}'";
        for (WhereScriptDTO dtos : dto.getWhereScriptDTOS()) {
            String nowDate = "";    //对应格式当前时间
            String twoDate = "";  //对应格式同步范围前时间
            if (StrUtil.isNotBlank(dtos.getSourceQueryCriteria())) {
                if (dtos.getSourceQueryCriteria().contains("as int")) {
                    DateDataDTO dateDataDTO = new DateDataDTO();
                    dateDataDTO.setDbName(dtos.getSourceDbName());
                    dateDataDTO.setTbName(dtos.getSourceTableName());

                    String str = dtos.getSourceQueryCriteria().replaceAll("where", "");
                    String str1 = str.replaceAll("cast\\(", "").replaceAll("\\)", "");

                    if (str1.contains("as string")) {
                        String[] str2 = str1.split("as string");
                        dateDataDTO.setSouColName(str2[0].replaceAll(" ", ""));
                        //判断是否包含as int
                        String[] str3 = str2[1].split("as int");
                        String[] str4 = str3[0].split("and");
                        String str5 = str4[0].replaceAll("\\'", "").replaceAll("=", "").replaceAll(" ", "");
                        dateDataDTO.setSource(str5);
                        dateDataDTO.setDateColName(str4[1].replaceAll(" ", ""));
                        String[] str6 = str3[1].replaceAll("BETWEEN ", "").split("and");
                        twoDate = str6[0].replaceAll(" ", "");
                        nowDate = str6[1].replaceAll(" ", "");
                    } else {
                        String[] str3 = str1.split("as int");
                        dateDataDTO.setDateColName(str3[0].replaceAll(" ", ""));
                        String[] str6 = str3[1].replaceAll("BETWEEN ", "").split("and");
                        twoDate = str6[0].replaceAll(" ", "");
                        nowDate = str6[1].replaceAll(" ", "");
                    }
                    String tarDropSql = "";
                    String auditSqlJQ = "";
                    String auditSqlWG = "";
                    //判断有没有源分区
                    if (StrUtil.isNotBlank(dateDataDTO.getSouColName())) {
                        String delColSql = StrUtil.format(delCol, dateDataDTO.getSouColName(), dateDataDTO.getSource());
                        String auditColSql = StrUtil.format(auditCol, dateDataDTO.getSouColName(), dateDataDTO.getSource());
                        tarDropSql = StrUtil.format(dropSql, tarPrefix + dateDataDTO.getDbTbName(), delColSql, dateDataDTO.getDateColName(), twoDate);
                        //数据稽核sql
                        auditSqlJQ = StrUtil.format(auditSql, dateDataDTO.getDbTbName(), auditColSql,
                                dateDataDTO.getDateColName(), twoDate, nowDate);
                        auditSqlWG = StrUtil.format(auditSqlNot, tarPrefix+dateDataDTO.getDbTbName(), auditColSql);
                    } else {
                        tarDropSql = StrUtil.format(dropSql, tarPrefix + dateDataDTO.getDbTbName(), "", dateDataDTO.getDateColName(), twoDate);
                        //数据稽核sql
                        auditSqlJQ = StrUtil.format(auditSql, dateDataDTO.getDbTbName(),  "",
                                dateDataDTO.getDateColName(), twoDate, nowDate);
                        auditSqlWG = StrUtil.format(auditSqlNot, tarPrefix+dateDataDTO.getDbTbName(),  "");

                    }
                    //删除近2年
                    String message = impalaService.dele(tarDropSql);
                    HotSpareDele hotSpareDele = new HotSpareDele();
                    hotSpareDele.setTaskId(hotSpareDeleTask.getId());
                    hotSpareDele.setTaskName(hotSpareDeleTask.getTaskName());
                    hotSpareDele.setDeleType(ScanCommonConstants.ClusterName.DELETE.value);
                    hotSpareDele.setLibraryTableName(dateDataDTO.getDbTbName());
                    hotSpareDele.setSourceName(dto.getSourceDataName());
                    hotSpareDele.setSql(tarDropSql);
                    if(StrUtil.isNotBlank(message)){
                        if(!"成功".equals(message)){
                            hotSpareDele.setLog(message);
                            hotSpareDele.setDeleState(ScanCommonConstants.ClusterName.SB.value);
                        }else{
                            hotSpareDele.setDeleState(ScanCommonConstants.ClusterName.CG.value);
                        }
                    }
                    boolean sab = hotSpareDeleService.save(hotSpareDele);
                    //删除成功 才做稽核
                    if(ScanCommonConstants.ClusterName.CG.value.equals(hotSpareDele.getDeleState())&&sab){
                        //稽核金桥万国数据量(需要删除的表)
                        HotSpareDele hotSpareDeleAudit = new HotSpareDele();
                        hotSpareDeleAudit.setTaskId(hotSpareDeleTask.getId());
                        hotSpareDeleAudit.setTaskName(hotSpareDeleTask.getTaskName());
                        hotSpareDeleAudit.setDeleType(ScanCommonConstants.ClusterName.AUDIT.value);
                        hotSpareDeleAudit.setLibraryTableName(dateDataDTO.getDbTbName());
                        hotSpareDeleAudit.setSourceName(dto.getSourceDataName());
                        hotSpareDeleAudit.setSql("金桥稽核【"+auditSqlJQ+"】"+"万国稽核【"+auditSqlWG+"】");
                        Map<String,Object> mapJQ = impalaService.count(auditSqlJQ);
                        Map<String,Object> mapWG = impalaService.count(auditSqlWG);
                        String auditMessageJQ = (String)mapJQ.get("error");
                        String auditMessageWG = (String)mapWG.get("error");
                        if(StrUtil.isNotBlank(auditMessageJQ)||StrUtil.isNotBlank(auditMessageWG)){
                            hotSpareDeleAudit.setLog(auditMessageJQ+"======="+auditMessageWG);
                            hotSpareDeleAudit.setDeleState(ScanCommonConstants.ClusterName.SB.value);
                        }else{
                            Long countJQ  = Long.valueOf(String.valueOf(mapJQ.get("count")));
                            Long countWG  = Long.valueOf(String.valueOf(mapWG.get("count")));
                            hotSpareDeleAudit.setLog("金桥数据量为【"+countJQ+"】"+"万国数据量为【"+countWG+"】");
                            if(countJQ.equals(countWG)){
                                hotSpareDeleAudit.setDeleState(ScanCommonConstants.ClusterName.CG.value);
                            }else{
                                hotSpareDeleAudit.setDeleState(ScanCommonConstants.ClusterName.SB.value);
                            }
                        }
                        hotSpareDeleService.save(hotSpareDeleAudit);
                    }
                }
            }
        }
        return true;
    }
}
